<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var combinationSum2 = function (candidates, target) {
      candidates.sort((a, b) => a - b)
      var result = [], n = candidates.length
      function backtrack(start, sum, list) {
        if (sum === target) {
          result.push(list)
        }
        for (let i = start; i < n; i++) {
          if (candidates[i] + sum > target) break;
          //相同数字只允许循环的第一个递归，避免重复
          if (candidates[i] === candidates[i - 1] && i > start) continue;
          backtrack(i + 1, sum + candidates[i], [...list, candidates[i]])
        }
      }

      backtrack(0, 0, [])

      return result
    }

    console.log(combinationSum2(candidates = [10, 1, 2, 7, 6, 1, 5], target = 8))
  </script>
</body>

</html>